import requests
import sqlite3
import os
# 数据库初始化函数
def init_database():
    # 检查数据库文件是否存在，如果存在则删除
    if os.path.exists('user_database.db'):
        os.remove('user_database.db')

    # 连接数据库（如果不存在会自动创建）
    conn = sqlite3.connect('user_database.db')
    cursor = conn.cursor()

    # 创建user_info表
    cursor.execute('''
    CREATE TABLE user_info (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        username TEXT NOT NULL UNIQUE,
        password TEXT NOT NULL,
        token TEXT NOT NULL UNIQUE
    )
    ''')

    # 插入默认测试用户
    default_users = [
        ('test', '123456', '4AB527C4318B39FB74B4BA1ABBCC226B'),
        ('test2', '654321', '3AB527C4318B39FB74B4BA1ABBCC226A')
    ]

    cursor.executemany('INSERT INTO user_info (username, password, token) VALUES (?, ?, ?)', default_users)

    # 提交更改并关闭连接
    conn.commit()
    conn.close()


# 在模块导入时初始化数据库
init_database()


class UserGL:
    @classmethod
    def assert_user(cls, resp, status_code, success, message):
        assert resp.status_code == status_code  # 修改这里，使用传入的status_code而不是硬编码的201
        assert resp.json().get("success") is success
        assert resp.json().get("message") == message
        assert resp.json().get("code") == status_code

    @classmethod
    def user_zc(cls, data):
        resp = requests.post(url="/api/register", headers={"Content-Type": "application/json"}, json=data)
        return resp

    @classmethod
    def user_dl(cls, data):
        resp = requests.post(url="/api/login", headers={"Content-Type": "application/json"}, json=data)
        return resp


class Address:
    @classmethod
    def assert_address(cls, resp, success, message):
        assert resp.json().get("success") is success
        assert resp.json().get("message") == message

    @classmethod
    def assert_address_select(cls, resp, success):
        assert resp.json().get("success") is success

    @classmethod
    def add_address(cls, data):
        token = UserGL.user_dl(data).json().get("data").get("token")
        resp = requests.post(
            url="/address/add",
            headers={"Content-Type": "application/json", "Authorization": token},
            json=data
        )
        return resp

    @classmethod
    def update_address(cls, data):
        token = UserGL.user_dl(data).json().get("data").get("token")
        resp = requests.post(
            url="/address/update",
            headers={"Content-Type": "application/json", "Authorization": token},
            json=data
        )
        return resp

    @classmethod
    def delete_address(cls, data):
        token = UserGL.user_dl(data).json().get("data").get("token")
        resp = requests.post(
            url="/address/delete",
            headers={"Content-Type": "application/json", "Authorization": token},
            json=data
        )
        return resp

    @classmethod
    def get_address(cls, data):
        token = UserGL.user_dl(data).json().get("data").get("token")
        resp = requests.post(
            url="/address/list",
            headers={"Content-Type": "application/json", "Authorization": token},
            json=data
        )
        return resp

    @classmethod
    def mr_address(cls, data):
        token = UserGL.user_dl(data).json().get("data").get("token")
        resp = requests.post(
            url="/address/set_default",
            headers={"Content-Type": "application/json", "Authorization": token},
            json=data
        )
        return resp